<script>
	import Vue from 'vue'
	import api from 'utils/api'
	import util from 'utils/util'
	import __config from 'config/env';

	export default {
		globalData: {
			tenantId: 1, //租户Id
			// appId: 'wx64d6f621906b9626', //公众号appId
			appId: 'wx2feca2c1b14ba3c4', //公众号appId
			noPic:'https://fs-mix.oss-cn-beijing.aliyuncs.com/1/material/cd4c4797-a491-4246-9ecf-6c6251a484e7.png',
			theme: { //主题定义
				backgroundColor: 'gradual-lPink',
				themeColor: 'red',
				tabbarBackgroundColor: '#ffffff',
				tabbarColor: '#666666',
				tabbarSelectedColor: '#e53c43',
				tabbarBorderStyle: '#black',
				tabbarItem: []
			}
		},
		onShow(opt) {
			
		},
		onLaunch() {
			//小程序平台检测新版本
			uni.setStorageSync('yc_actName',null)
			
			let startParamObj = uni.getLaunchOptionsSync()
			if (!startParamObj || startParamObj.scene != 1154) {
				this.updateManager();
				// this.doLogin();
			}

			//设置全局样式
			this.setGlobalStyle()

			// 获取总仓
			this.getMainStore()
			
			// 清除third_session重新登录
			uni.removeStorageSync('third_session')
		},

		methods: {
			//设置全局样式
			setGlobalStyle() {
				uni.getSystemInfo({
					success: (e) => {
						Vue.prototype.StatusBar = e.statusBarHeight;
						let capsule = wx.getMenuButtonBoundingClientRect();
						if (capsule) {
							Vue.prototype.Custom = capsule;
							// Vue.prototype.capsuleSafe = uni.upx2px(750) - capsule.left + uni.upx2px(750) - capsule.right;
							Vue.prototype.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
						} else {
							Vue.prototype.CustomBar = e.statusBarHeight + 50;
						}
					}
				})
				let that = this;
				//获取主题装修配置
				api.themeMobileGet().then(res => {
					let themeMobile = res.data;
					const isGray = themeMobile.isGray ? themeMobile.isGray : 0
					uni.setStorageSync('home_isGary', isGray)
					//将默认配置换成后台数据
					if (themeMobile) {
						that.globalData.theme.themeColor = themeMobile.themeColor
						that.globalData.theme.backgroundColor = themeMobile.backgroundColor
						that.globalData.theme.tabbarBackgroundColor = themeMobile.tabbarBackgroundColor
						that.globalData.theme.tabbarColor = themeMobile.tabbarColor
						that.globalData.theme.tabbarSelectedColor = themeMobile.tabbarSelectedColor
						that.globalData.theme.tabbarBorderStyle = themeMobile.tabbarBorderStyle
					}
					if (themeMobile && themeMobile.tabbarItem) {
						let tabbarItem = [{
							index: 0
						}, {
							index: 1
						}, {
							index: 2
						}, {
							index: 3
						}]
						let tabbarItemInfo = isGray ? themeMobile.tabbarItem.info2 : themeMobile.tabbarItem.info1
						if (tabbarItemInfo.length > 0) {
							tabbarItemInfo.forEach(item => {
								if (item.text) tabbarItem[item.index].text = item.text
								if (item.iconPath) tabbarItem[item.index].iconPath = item.iconPath
								if (item.selectedIconPath) tabbarItem[item.index].selectedIconPath = item
									.selectedIconPath
							})
							that.globalData.theme.tabbarItem = tabbarItem
						}
					}
					uni.setStorageSync('tabbarItem', that.globalData.theme.tabbarItem)
					that.setTabBar()
				});
			},

			//设置tabbar
			setTabBar() { 
				let themeMobile = this.globalData.theme 
				if (themeMobile.tabbarBackgroundColor) {
					//动态设置 tabBar 的整体样式
					uni.setTabBarStyle({
						backgroundColor: themeMobile.tabbarBackgroundColor,
						color: themeMobile.tabbarColor,
						selectedColor: uni.getStorageSync('home_isGary') ? '#333' : themeMobile
							.tabbarSelectedColor,
						borderStyle: themeMobile.tabbarBorderStyle
					});
					//动态设置 tabBar 某一项的内容					
					let tabbarItem = themeMobile.tabbarItem&&themeMobile.tabbarItem.length ? themeMobile.tabbarItem : uni.getStorageSync('tabbarItem')? uni.getStorageSync('tabbarItem') : []
					tabbarItem.forEach(item => {
						uni.setTabBarItem({
							index: item.index,
							text: item.text,
							iconPath: item.iconPath,
							selectedIconPath: item.selectedIconPath
						});
					})
				}
			},


			//页面初始化方法，供每个页面调用
			initPage() {
				let that = this;
				return new Promise((resolve, reject) => {
					that.setTabBar()
					// #ifdef MP
					//小程序平台检测新版本
					let startParamObj = uni.getLaunchOptionsSync()
					if (startParamObj.scene == 1154) {
						resolve("success")
					}
					// #endif					
					// 登录校验
					if (!uni.getStorageSync('third_session')) {
						//无thirdSession，进行登录
						that.doLogin().then(res => {
							resolve("success");
						});
					} else {
						//小程序需要检查登录态是否过期
						if (util.isMiniPg()) {
							uni.checkSession({
								success() {
									//session_key 未过期，并且在本生命周期一直有效
									// console.log('session_key 未过期')
									api.userInfoGet().then(res => {
										if (res.data) {
											uni.setStorageSync('user_info',  res.data)
										}
									})
									resolve("success");
								},
								fail() {
									// session_key 已经失效，需要重新执行登录流程
									that.doLogin().then(res => {
										resolve("success");
									});
								}
							})
						} else {
							resolve("success");
						}
					}
				});
			},

			//登录操作
			doLogin() {
				uni.showLoading({
					title: '登录中'
				});
				return new Promise((resolve, reject) => {
					//微信小程序登录					
					this.loginWxMa().then(res => {
						resolve("success");
					});
				});
			},
			//微信小程序登录
			loginWxMa() {
				return new Promise((resolve, reject) => {
					uni.login({
						success: (res) => {
							if (res.code) {
								api.loginWxMa({
									jsCode: res.code
								}).then(res => {
									uni.hideLoading();
									let userInfo = res.data	|| {}
									uni.setStorageSync('third_session', userInfo.thirdSession);
									uni.setStorageSync('user_info', userInfo);
									// 判断当前页面 跳转不同首页
									var pages = getCurrentPages(); // 获取页面栈
									var currPage = pages[pages.length - 1];
									if(userInfo.userType=='3'&& currPage && currPage.route=='pages/home/index') {
										uni.redirectTo({
											url:'/pages/actYc/act-yc'
										})
									}					
									resolve("success");
								});
							}
						}
					});
				});
			},

			// 获取总仓
			getMainStore() {
				api.getCenterShop('2').then(res => {
					uni.setStorageSync('M_mainstore', res.data);
				})
				api.getCenterShop('6').then(res1 => {
					uni.setStorageSync('Y_mainstore', res1.data);
				})
			},

			updateManager() {
				const updateManager = uni.getUpdateManager();
				updateManager.onUpdateReady(function() {
					uni.showModal({
						title: '更新提示',
						content: '新版本已经准备好，是否重启应用？',
						success(res) {
							if (res.confirm) {
								updateManager.applyUpdate();
							}
						}
					});
				});
			},

			checkLogin(pageType) {
				return new Promise((resolve, reject) => {
					const userInfo = uni.getStorageSync('user_info')
					if (!userInfo.phone) {
						// 未登录,弹出登录
						if (pageType == 'tabbar'||pageType == 'checkLogin') {
							uni.reLaunch({
								url: '/pages/home/index?login=true'
							})
						}
						reject()
					} else if (userInfo.phone && userInfo.userLevelId == 1 && pageType!='checkLogin' && pageType!='yc') {
						// 游客禁止跳转
						uni.showModal({
							title: '提示',
							content: '抱歉，您的账号权限为游客，无法进行浏览',
							showCancel: false,
							success(res) {}
						});
						if (pageType == 'tabbar') {
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/home/index'
								});
							}, 500)
						}
					} else if(userInfo.phone && pageType=='yc' && !(userInfo.enable !='3' && userInfo.userType == 3)){
						// 云仓分销员判断
						uni.showModal({
							title: '提示',
							content: '抱歉，您的账号权限为游客，无法进行浏览',
							showCancel: false,
							success(res) {}
						});
						reject('userInfo')
					}else {
						// 正常操作
						resolve()
					}
				})
			}
		}
	};
</script>
<style>
	@import "./app.css";
</style>